﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Microsoft.Web.Mvc;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class RoomController : Controller
    {
        private readonly IRoomRepository roomRepository;
        private readonly IRoomStateRepository roomStateRepository;
        private readonly IRoomTypeRepository roomTypeRepository;
        private readonly IFloorRepository floorRepository;
        private readonly IUnitOfWork unitOfWork;
        public RoomController(IRoomRepository roomRepository, IRoomStateRepository roomStateRepository, IUnitOfWork unitOfWork, IRoomTypeRepository roomTypeRepository, IFloorRepository floorRepository)
        {
            this.roomRepository = roomRepository;
            this.roomStateRepository = roomStateRepository;
            this.roomTypeRepository = roomTypeRepository;
            this.floorRepository = floorRepository;
            this.unitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            //先设定一个值，供后面显示层用
            Session["number"] = "0";
            Session.Remove("roomId");
            Session.Remove("roomTypeId");
            Session.Remove("clientId");

            ViewBag.FreeStateCount = roomRepository.GetRoomNumberByState(1);
            ViewBag.UsedStateCount = roomRepository.GetRoomNumberByState(2);
            ViewBag.RepairStateCount = roomRepository.GetRoomNumberByState(3);
            ViewBag.CleanStateCount = roomRepository.GetRoomNumberByState(4);
            ViewBag.BookStateCount = roomRepository.GetRoomNumberByState(5);
            var rooms = roomRepository.GetAll().ToList();
            return View(rooms);
        }

        private IList<RoomInfoModel> GetRoomByState(int id)
        {
           
            var rooms = roomRepository.GetRoomByState(id).AsQueryable();

            IList<RoomInfoModel> roleModels =
                (from r in rooms
                 select new RoomInfoModel
                 {
                     ID = r.ID,
                     Name = r.Name,
                     BedNum = r.BedNum ,
                     RoomStateName = r.RoomState.Name,
                     RoomTypeName = r.RoomType.Name,
                     FloorName = r.Floor.Name
                 }).ToList();
            return roleModels;
        }
        //更具房态显示房间
        public ActionResult ShowRoomByState(int id)
        {
            GetData();
            Session["stateId"] = id;
            var roomByState= GetRoomByState(id);
            return View(roomByState);
        }
        [GridAction]
        public ActionResult TelerikAjax()
        {
            GetData();
            int i = Convert.ToInt32(Session["stateId"]);
            return View(new GridModel(GetRoomByState(i)));
        }
        //更改房态
        [HttpPost]
        public bool ChangeRoomState(string id, int roomStateId)
        {
            var room = roomRepository.GetById(id);
            if (room != null)
            {
                room.RoomState = roomStateRepository.Get(x => x.ID == roomStateId);
                roomRepository.Update(room);
                unitOfWork.Commit();
                return true;
            }
            return false;
        }






        //以下是实现对房间的操作
        private IList<RoomInfoModel> GetAllRoom()
        {
            var rooms = roomRepository.GetAll().AsQueryable();
            IList<RoomInfoModel> roomInfoModels =
                (from r in rooms
                 select new RoomInfoModel
                 {
                     ID = r.ID,
                     Name = r.Name,
                     BedNum = r.BedNum,
                     RoomStateName = r.RoomState.Name,
                     RoomTypeName = r.RoomType.Name,
                     FloorName = r.Floor.Name
                 }).ToList();
            return roomInfoModels;
        }
        public ActionResult ShowAllRoom()
        {
            GetData();
            return View(GetAllRoom());
        }

        [GridAction]
        public ActionResult TelerikAjaxToShow()
        {
            GetData();
            var md = new GridModel(GetAllRoom());
            
            return View(md);
        }

        [HttpPost]
        [GridAction]
        public ActionResult AddRoom(string Id, string Name, int BedNum, int RoomTypeName, int FloorName, int RoomStateName)
        {

            var room = new Room();
            room.ID = Id;
            room.Name = Name;
            room.BedNum = BedNum;
            room.RoomType = roomTypeRepository.Get(x => x.ID == RoomTypeName);
            room.Floor = floorRepository.Get(x => x.ID == FloorName);
            room.RoomState = roomStateRepository.Get(x => x.ID == RoomStateName);

            if (TryUpdateModel(room))
            {
                roomRepository.Add(room);
                unitOfWork.Commit();
            }
            return View(new GridModel(GetAllRoom()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult DeleteRoom(string Id)
        {
            var model = roomRepository.Get(x => x.ID == Id);
            if (model != null)
            {
                roomRepository.Delete(model);
                unitOfWork.Commit();
            }
            return View(new GridModel(GetAllRoom()));
        }
        [HttpPost]
        [GridAction]
        public ActionResult UpdateRoom(string Id, string Name,int BedNum, int RoomTypeName, int FloorName, int RoomStateName)
        {
            var room = roomRepository.Get(x => x.ID == Id);

            room.Name = Name;

            room.BedNum = BedNum;

            room.Floor = floorRepository.Get(e => e.ID == FloorName);
                                        
            room.RoomType = roomTypeRepository.Get(e => e.ID == RoomTypeName);

            room.RoomState = roomStateRepository.Get(e => e.ID == RoomStateName);

            if (TryUpdateModel(room))
            {
                roomRepository.Update(room);
                unitOfWork.Commit();
            }
            return View(new GridModel(GetAllRoom()));
        }

        //获得绑定的数据源
        public void GetData() 
        {
            ViewData["roomTypeNames"] = roomTypeRepository.GetAll().ToList();
            ViewData["roomStateNames"] = roomStateRepository.GetAll().ToList();
            ViewData["floorNames"] = floorRepository.GetAll().ToList();
        }

        
    }
}
